Creating a Query-based Custom Class |
|
This option helps you to build a custom class using a query. Using the query-builder of WS-AppServer, you can bind database objects together in a query string and make it executable. Methods generated on such classes run in the form of a query and thus automate data retrieval.
- Double-click the <WS-AppServer Package> from either of the following locations:
- Workspace Documents (My Recent Documents) window.
- Workspace Documents (Explorer) > <solution> > <project> . The WS-AppServer Package window appears, displaying the data models and their corresponding attributes, methods, and relations.
- On the WS-AppServer Package toolbar, click . The Create Class from Query window appears.
- Browse and select the Database Metadata from the Select Database Metadata list. The tables available in the selected Database Metadata appear under DB Object.
- Select the required tables and click Next.
Note: Use the navigation buttons to see all the tables. You can select more than one table or all the tables, to build the query. To view database in its view format, select Views. - To build the query, type it in the empty space.
Note: It supports SQL query structure, where you can provide the relevant details depending upon the field selection, to build the query. It is mandatory to provide a valid query. - Click Next to proceed and do one of the following:
- If it is a derived class, select the class from the Derived From list.
- If it is not a derived class, provide a name for the class in the View Class Name field.
- Click Finish.
The query is built and the custom class based on the query is created in the Models group box.
Example
You want to create a query custom class that will retrieve shipping location details of a particular order for a customer and also retrieve the date of placing the order. The important fields that will help in getting this information are CustomerID, OrderID, OrderDate, and ShipCity. Since this information is not available in a single table, you have to select two tables -
Customers and
Orders. To create the query custom class on this logic, do the following:
- On the Create Class from Query window, from the DB Object table, select Customers and Orders and click Next.
- In the space for the query, type the following query and click Next.
SELECT Customers.CustomerID, Customers.City, Orders.OrderDate, Orders.ShipCity From Customers, Orders WHERE Customers.CustomerID= Orders.CustomerID
where, CustomerID and City are from the Customers table, and OrderDate and ShipCity are from the Orders table. Also, CustomerID exists in the Orders table as Foreign Key. - Under Derived From, select Customers because that contains the primary key CustomerID which will be used to retrieve the details. CustomersView appears in the View Class Name field.
- Click Finish. A message appears indicating successful creation of the query custom class.
- On the WS-AppServer Package editor, under Models, search for the CustomersView query custom class (indicated by ) and select it. The attributes (City, CustomerID, OrderDate, and ShipCity) of that class appear on the Attributes tab.
- Under Models, right-click the CustomersView query custom class and select Add > Standard Methods.
- On the Add Standard Methods dialog box, select the attribute ShipCity (because you need the shipping location details) and provide a name for the Object.
- Select the Does the selected attribute(s) uniquely identify an object? check box if you want to treat the object as unique. If you select this, you can retrieve details of an object based on the selected attribute(s) alone.
- Click OK. The methods are generated and displayed on the Methods tab.
Now that you have generated the CustomersView query custom class and have added Standard Methods to it, you have to generate Java code followed by Web service operations for the CustomersView query custom class, to make it functional.
After you complete this task:
Select the query-based custom class that you created and add methods to it. Subsequently, generate Java code and Web service operations on it and publish the Web service operations to an organization, so that you can use them at run time.
Tip: !!Click here to watch the video.